import {
  SAVE_ALL_CHANNEL,
  SAVE_CHANNEL,
  SAVE_ARTICLELIST,
  SAVE_MORE_ARTICLE_LIST,
} from '../actionTypes/home'

const initValue = {
  userChannels: [],
  allChannels: [],
  articles: {},
}

export default function reducer(state = initValue, action) {
  switch (action.type) {
    case SAVE_CHANNEL:
      return {
        ...state,
        userChannels: action.payload,
      }
    case SAVE_ALL_CHANNEL:
      return {
        ...state,
        allChannels: action.payload,
      }
    case SAVE_ARTICLELIST:
      return {
        ...state,
        articles: {
          ...state.articles,
          [action.payload.channelId]: {
            timestamp: action.payload.timestamp,
            list: action.payload.list,
          },
        },
      }
    case SAVE_MORE_ARTICLE_LIST:
      return {
        ...state,
        articles: {
          ...state.articles,
          [action.payload.channelId]: {
            timestamp: action.payload.timestamp,
            list: [
              ...state.articles[action.payload.channelId].list,
              ...action.payload.list,
            ],
          },
        },
      }
    default:
      return state
  }
}
